using SQLDMO;
using SQLServices.Repository;

namespace SQLRunnerDMO
{
	/// <summary>
	/// Simplified class version of a SQL DMO Database class
	/// </summary>
	public sealed class SQLDatabase: ISQLDatabase
	{

		#region Private Instances

		private _Database _dmoDatabase;
		private SQLConnection _sqlConnection;
		private string _databaseName;

		#endregion
		#region Constructors

		public SQLDatabase(SQLConnection sqlConnection, string databaseName)
		{
			// Check if SQL Server instance is available
			_sqlConnection = sqlConnection;
			_databaseName = databaseName;
			_dmoDatabase = _sqlConnection.Database(_databaseName);
			
		}

		#endregion
		#region Properties
		
		public string DatabaseName
		{
			get { return _databaseName; }
		}

		public ISQLConnection SQLConnection
		{
			get { return _sqlConnection; }
		}

		#endregion
		#region Public Methods

		public ISQLDatabase Clone()
		{
			SQLConnection newConnection = new SQLConnection();
			newConnection.OpenConnection(_sqlConnection.ServerName,
				_sqlConnection.UserId, _sqlConnection.Password, _sqlConnection.LoginSecure);
			return new SQLDatabase(newConnection, _databaseName);
		}

		public void Execute(string sqlScript)
		{
			_dmoDatabase.ExecuteImmediate(
				sqlScript, SQLDMO_EXEC_TYPE.SQLDMOExec_Default, sqlScript.Length);
		}

		#endregion

	}
}
